const path = require('path')
const fs = require('fs')
const Stacktracey = require('stacktracey')
const sourceMap = require('source-map')
//读取文件
 const readFile = (filePath) =>
  new Promise((resolve, reject) => {
    // 以读取模式打开文件
    fs.readFile(filePath, { flag: "r" }, (err, data) => {
      if (err) reject(err);
      else resolve(data.toString());
    });
  });
 const init = async () => {
  return
    const esk ="{\n \"name\": \"TypeError\",\n \"message\": \"Cannot destructure property 'total' of 'a.data' as it is null.\",\n \"stack\": \"TypeError: Cannot destructure property 'total' of 'a.data' as it is null.\\n at http://localhost:8080/assets/applyEntry.9667136a.js:1:955\"\n}"

    const tracey = new Stacktracey(esk ?? {});
const errorInfo = tracey.items?.[0] ?? {}

console.log(JSON.stringify(errorInfo))
console.log(errorInfo)
  const sourceMapFileContent = await readFile(
    path.resolve(
      __dirname,
      `./public/sourcemap/applyEntry.9667136a.js.map`
    )
  );
  const sourceMapContent = JSON.parse(sourceMapFileContent);
  const consumer = await new sourceMap.SourceMapConsumer(sourceMapContent);
  const originalPosition = consumer.originalPositionFor({
   line: errorInfo.line,
   column: errorInfo.column
  });
  console.log(originalPosition)
   console.log(JSON.stringify(originalPosition))
  sourcePosition = originalPosition?.source || "";
};
module.exports = {init}